/*
 *  @author GeneZhao   
 *  @date   2015-04-07 16:23:18
 *  @desc   
 */
Ext.define('kmsApp.view.main.MainController', {
    extend: 'Ext.app.ViewController',

    requires: [
        'Ext.window.MessageBox', 'Ext.window.Toast'
    ],

    alias: 'controller.main',

    control: {
        'mainmenutree': {
            selectionchange: 'onTreeNavSelectionChange'
        }
    },

    routes: {
        ':id': {
            action: 'handleRoute1',
            before: 'beforeHandleRoute1'
        }
    },

    handleRoute1: function(id) {
        var me = this,
            mainView = me.getView(),
            navigationTree = mainView.down('mainmenutree'),
            contentPanel = mainView.down('app-contentpanel'),
            store = navigationTree.getStore(),
            node = store.getNodeById(id),
            className,cmp,ViewClass;
        /*if (node.isLeaf()) {
            Ext.Msg.alert(
                '提示',
                '当前点击的是叶子节点，右侧panel将跳转到对应的组件上');
        } else {
            Ext.Msg.alert(
                '提示',
                '当前点击的是非叶子节点，右侧panel将跳转到导航界面上');
        }*/
        //响应路由
        navigationTree.getSelectionModel().select(node);
        navigationTree.getView().focusNode(node);

        //响应路由，改变右侧panel内容
        contentPanel.removeAll(true);
        if (node.isLeaf()) {
            ViewClass = Ext.ClassManager.getByAlias('widget.'+id);
            // ViewClass = Ext.ClassManager.get(className);
            cmp = new ViewClass();
            contentPanel.add(cmp);
        }
        var text = node.get('text'),
            title = node.isLeaf() ? (node.parentNode.get('text') + ' - ' + text) : text;
        contentPanel.setTitle(title);
        document.title = document.title.split(' - ')[0] + ' - ' + text;
    },

    beforeHandleRoute1: function(id, action) {
        var me = this,
            mainView = me.getView(),
            navigationTree = mainView.down('mainmenutree'),
            store = navigationTree.getStore();
        var node = store.getNodeById(id);

        if (node) {
            action.resume();
        } else if (store.getCount() === 0) {
            store.on('load', function() {
                node = store.getNodeById(id);
                if (node) {
                    action.resume();
                } else {
                    Ext.Msg.alert('路由跳转失败', '找不到id为' + id + ' 的组件');
                    action.stop();
                }
            });
        } else {
            Ext.Msg.alert(
                '路由跳转失败',
                '找不到id为' + id + ' 的组件. 界面将跳转到应用初始界面',
                function() {
                    me.redirectTo('all');
                }
            );
            //stop action  
            action.stop();
        }
    },

    handleRoute: function(id) {
        var me = this,
            mainView = me.getView(),
            navigationTree = mainView.down('mainmenutree'),
            contentPanel = mainView.down('maincenter'),
            store = navigationTree.getStore(),
            node = store.getNodeById(id),
            className, cmp, ViewClass;
        console.log(node)
            //响应路由，左侧树定位到相应节点
        navigationTree.getSelectionModel().select(node);
        navigationTree.getView().focusNode(node);

        //响应路由，改变右侧panel内容
        contentPanel.removeAll(true);
        if (node.isLeaf()) {
            className = Ext.ClassManager.getNameByAlias('widget.' + id);
            ViewClass = Ext.ClassManager.get(className);
            cmp = new ViewClass();
            contentPanel.add(cmp);
        }
        var text = node.get('text'),
            title = node.isLeaf() ? (node.parentNode.get('text') + ' - ' + text) : text;
        contentPanel.setTitle(title);
        document.title = document.title.split(' - ')[0] + ' - ' + text;
    },

    beforeHandleRoute: function(id, action) {
        var me = this,
            mainView = me.getView(),
            navigationTree = mainView.down('mainmenutree'),
            store = navigationTree.getStore(),
            node = store.getNodeById(id);

        if (node) {
            action.resume();
        } else if (store.getCount() === 0) {
            //在store load事件中判断节点，避免store数据未加载情况
            store.on('load', function() {
                console.log('load')
                node = store.getNodeById(id);
                if (node) {
                    action.resume();
                } else {
                    Ext.Msg.alert('路由跳转失败', '找不到id为' + id + ' 的组件');
                    action.stop();
                }
            });
        } else {
            Ext.Msg.alert('路由跳转失败', '找不到id为' + id + ' 的组件');
            action.stop();
        }
    },
    onClickButton: function() {
        Ext.Msg.confirm('Confirm', 'Are you sure?', 'onConfirm', this);
    },

    onConfirm: function(choice) {
        if (choice === 'yes') {
            //
        }
    },
    // 选择了主菜单上的菜单后执行
    onMainMenuClick: function(menuitem) {
        console.log(menuitem);
        Ext.toast({

            html: 'Data Saved , hello  this is a meessage',
            title: menuitem.text,
            saveDelay: 10,
            align: 'tr',
            closable: true,
            width: 200,
            useXAxis: true,
            slideInDuration: 500
        });
    },

    onTreeNavSelectionChange: function(selModel, records) {
        var record = records[0];
        if (record) {
            this.redirectTo(record.getId());
        }
    }
});
